﻿@{
    ViewData["Title"] = "Home Page";
    var id = ViewBag.ID.ToString();
    var getid = id == "hzy" ? "wjf" : "hzy";
}

<div class="container">

    <div class="row">
        <div class="col-sm-12" style="background:red; color:#fff; padding:10px;font-size:25px;">@id ，您好欢迎登陆</div>
        <div class="col-sm-9" style="background:black;color:#fff; height:500px; overflow:auto;">
            <ul id="cmd"></ul>
        </div>

        <div class="col-sm-3">
            <div class="form-group">
                <label for="exampleInputEmail1">您的好友：</label>
                <input class="form-control" type="text" id="getid" value="@getid" />
            </div>

            <div class="form-group">
                <label for="exampleInputEmail1">消息内容：</label>
                <textarea class="form-control" type="text" id="msg" rows="15">我是默认消息</textarea>
            </div>

        </div>

        <div class="col-sm-12" style="margin-top:10px;">
            <button class="btn btn-primary" onclick="App.SendMsg()">发送</button>

            <button class="btn btn-danger" onclick="App.SendMsgAll()">全局广播发送</button>

            <button class="btn btn-warning" onclick="App.SendMsgTo50W()">定时发送消息</button>
        </div>

    </div>


</div>
@section Scripts{

    <script src="~/js/reconnecting-websocket.min.js"></script>
    <script type="text/javascript">

    var userid = '@id';

    var protocol = location.protocol === "https:" ? "wss:" : "ws:";
        var wsUri = protocol + "//" + window.location.host + "/" + (location.protocol === "https:" ? "wss" : "ws");

    var ws = new ReconnectingWebSocket(wsUri);//new WebSocket(wsUri);//new WebSocket("ws://localhost:44342/");

    //var ws = new WebSocket(wsUri);
    var msgCount = 0;
    ws.onopen = function () {
        console.log("open");
        App.Register();
    };

    ws.onmessage = function (evt) {
        msgCount++;
        //console.log(evt.data);
        //console.log(msgCount);
        var ele_li = document.createElement("li");
        ele_li.innerHTML = msgCount + "、" + evt.data;

        var ele_cmd = document.getElementById("cmd");
        if (ele_cmd.childNodes.length == 0) {
            ele_cmd.innerHTML = ele_li.outerHTML;
        }
        else {
            ele_cmd.insertBefore(ele_li, ele_cmd.childNodes[0]);
        }


        //$("#cmd").prepend(html);
    };

    ws.onclose = function (evt) {

        console.log("WebSocketClosed!");
        console.log(evt);

    };

    ws.onerror = function (evt) {

        console.log("WebSocketError!");

    };

    var App = {
        Register: function () {
            //注册
            ws.send(JSON.stringify({
                UserID: userid,
                ToUserID: null,
                Key: "Register",
                Message: "尊敬的用户“" + userid+ "“您已上线！"
            }));
        },
        SendMsg: function () {
            //给别人发消息
            ws.send(JSON.stringify({
                UserID: userid,
                ToUserID: document.getElementById("getid").value,
                Key: "SendToUser",
                Message: "【您的好友：" + userid + "说】：" + document.getElementById("msg").value
            }));
        },
        SendMsgAll: function () {
            //给别人发消息
            ws.send(JSON.stringify({
                UserID: userid,
                ToUserID: null,
                Key: "SendToUser",
                Message: "各小组注意了，这是一条全局广播"//$("#msg").val()
            }));
        },
        //发送50w 条消息 测试
        SendMsgTo50W: function () {
            App.SendMsg();
            var i = 0;
            var timer = setInterval(function () {
               App.SendMsg();
               i++;
           }, 1 * 500);

            if (i > 1000) {
                clearInterval(timer);
            }

        }
    };


    </script>
}
